{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27c8a441-adde-4f5e-ae54-8dac382ba4c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "cd_data = '.../Data/Daily/'\n",
    "\n",
    "df_firm_daily = pd.read_parquet(cd_data + 'Congress_tweets_daily.parquet')\n",
    "df_firm_daily['date']      = pd.to_datetime(df_firm_daily['date'])\n",
    "df_firm_count =pd.DataFrame(df_firm_daily.groupby(['date'])['permno'].count())\n",
    "df_firm_count = df_firm_count.rename(columns = {'permno':'nFirm'}).reset_index()\n",
    "\n",
    "# Scaled by total number of tweets\n",
    "df_all = pd.read_parquet(cd_data + 'Tweets_all.parquet')\n",
    "df_all['date']      = pd.to_datetime(df_all['Date'].dt.date)\n",
    "df_all_count =pd.DataFrame(df_all.groupby(['date'])['ID'].count())\n",
    "df_all_count = df_all_count.rename(columns = {'ID':'nTweets'}).reset_index()\n",
    "\n",
    "df_count = pd.merge(df_all_count, df_firm_count, how = 'left', on = 'date')\n",
    "df_count['nFirm'] = df_count['nFirm'].fillna(0)\n",
    "\n",
    "df_count['Ratio'] = df_count['nFirm']/df_count['nTweets'] * 100\n",
    "df_count['Ratio_smooth'] = df_count['Ratio'].rolling(window=30,  min_periods=4).mean()\n",
    "\n",
    "fig, axes = plt.subplots(1, 1 , figsize=(15,6), sharex=True, sharey=False)\n",
    "df_plot = df_count.copy()\n",
    "\n",
    "xdata = df_plot['date']\n",
    "ydata = df_plot['Ratio_smooth']\n",
    "plt.plot(xdata, ydata, '-',color='red', linewidth=3)\n",
    "\n",
    "ydata = df_plot['Ratio']\n",
    "plt.plot(xdata, ydata, '-',color = 'darkblue', alpha = 1, linewidth=3)\n",
    "plt.plot(xdata, ydata/ydata*0, '-k', linewidth=0.5)\n",
    "\n",
    "ydata = df_plot['Ratio_smooth']\n",
    "plt.plot(xdata, ydata, '-',color='red', linewidth=3)\n",
    "\n",
    "plt.ylabel('Percentage', fontsize='18')\n",
    "axes.spines['top'].set_visible(False)\n",
    "axes.spines['right'].set_visible(False)  \n",
    "axes.xaxis.set_tick_params(labelsize=16)\n",
    "axes.yaxis.set_tick_params(labelsize=16)\n",
    "\n",
    "plt.legend(labels=['Total of firm mentions scaled by total number of tweets (smoothed)'], loc='upper left'  , fontsize='18',frameon=False)\n",
    "\n",
    "\n",
    "plt.subplots_adjust(wspace=0.2, hspace=0.2)\n",
    "plt.margins(x=0, y=0)\n",
    "plt.ylim([0, 7])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
